package com.controller;


import com.entity.User;
import com.entity.table.UserTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/index")
public class LoginController {
    @GetMapping("")
    public String index(){
        return "client/index";
    }
//    @Autowired
//    FUserMapper UserMapper;
    @Autowired
    private UserService UserService;
        @GetMapping("/login")
    public String toLoginPage(Model model){
        return "client/login";
    }
    @PostMapping("/toLogin")
    public String login(User user, HttpSession httpSession, Model model){
//        FUser tUser = UserMapper.userLogin(user);
        QueryWrapper query=QueryWrapper.create()
                .where(UserTableDef.USER.USERNAME.eq(user.getUsername()))
                .and(UserTableDef.USER.PWD.eq(user.getPwd()));
        User tUser=UserService.getOne(query);
        if (tUser!=null){
            httpSession.setAttribute("user", tUser);
            model.addAttribute("user",tUser);
//            return "redirect:/";
            return "client/index";
        }
        else {
            model.addAttribute("msg","账号或密码错误,请重新输入！");
            return "client/login";
        }
    }
}